package flown.window;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.LayoutManager;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

import flown.window.ui.Widget;
import flown.window.ui.组件;


/**
 * @author flown
 */

public class Window
{
	private JFrame window = new JFrame();
	
	public Window(String title){this(title,800,600);}
	public Window(String title,int width,int height)
	{
		this.window.setTitle(title);									//设置窗口标题
		this.window.setSize(width, height);								//设置窗口宽高
		this.window.setLocationRelativeTo(null);						//设置窗口居中
		this.window.setLayout(new GridLayout());						//设置窗口布局
		this.window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);	//设置窗口默认CloseOperation
		this.window.setVisible(true);									//设置窗口为可视
	}
	
	
	/**
	 * 设置窗口为可视
	 * @param arg true or false
	 * */
	public final void SetVisible(boolean arg){this.window.setVisible(arg);}
	/**
	 * 设置窗口布局
	 * @param manager layout manager
	 * */
	public final void SetLayout(LayoutManager manager) {this.window.setLayout(manager);}
	
	
	/**
	 * 设置窗口图标
	 * @param path 文件路径
	 * */
	public final void SetIcon(String path) throws IOException
	{
		Image img = ImageIO.read(this.window.getClass().getResource(path));//读取路径图片
		if (img != null)
			this.window.setIconImage(img);//设置图片为窗口图标
		this.SetVisible(true);
	}
	/**
	 * 设置窗口图标
	 * @param input URL
	 * */
	public final void SetIcon(URL input) throws IOException
	{
		Image img = ImageIO.read(input);//读取图片
		this.window.setIconImage(img);//设置图片为窗口图标
		this.SetVisible(true);
	}
	/**
	 * 设置窗口图标
	 * @param img Image
	 * */
	public final void SetIcon(Image img){this.window.setIconImage(img);this.SetVisible(true);}
	
	
	/**
	 * 设置菜单栏
	 * @param menu_bar 菜单栏,menu bar
	 * */
	public final void SetMenuBar(MenuBar menu_bar)
	{
		this.window.setJMenuBar(menu_bar.GetMenuBar());
		this.window.setVisible(true);
	}
	
	/**
	 * 设置菜单栏
	 * @param menu_bar flown.window.菜单栏
	 * */
	public final void SetMenuBar(菜单栏 menu_bar)
	{
		this.window.setJMenuBar(menu_bar.获取菜单栏());
		this.window.setVisible(true);
	}
	
	
	/**
	 * 获取JFrame
	 * @return JFrame
	 * */
	public final JFrame GetFrame() {return window;}
	
	
	/**
	 * 添加鼠标监听器
	 * @param listener flown.window.listener.MouseListener
	 * */
	public final void AddMouseListener(flown.window.listener.MouseListener listener)
	{
		this.window.addMouseListener(listener);
		this.window.addMouseMotionListener(listener);
		this.window.addMouseWheelListener(listener);
	}
	
	/**
	 * 添加key监听器
	 * @param listener key listener
	 * */
	public final void AddKeyListener(KeyListener listener)
	{
		this.window.addKeyListener(listener);
	}
	
	/**
	 * 添加窗口监听器
	 * @param listener flown.window.listener.WindowListener
	 * */
	public final void AddWindowListener(flown.window.listener.WindowListener listener)
	{
		this.AddMouseListener(listener);
		this.AddKeyListener(listener);
		this.window.addWindowListener(listener);
	}
	
	
	/**
	 * window.Add
	 * @param comp java.awt.component
	 * */
	public final void Add(Component comp){this.window.getContentPane().add(comp);}
	public final void Add(Widget widget){this.window.getContentPane().add(widget.Get());}
	public final void Add(组件 widget){this.window.getContentPane().add(widget.获取());}
}






